#include<bits/stdc++.h>
using namespace std;
const int N=402;
template <typename T> inline void read(T &x) {
    x = 0; char ch = getchar(); int f = 1;
    while (!isdigit(ch) && ch ^ '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); x *= f;
}
bool sb=1,hv[N][N];
int n,a[N],u,v;
long long ans,b[N];
bool vis[N];
inline void sub19(int x,long long sum){
	if(x==n+1){
		ans=max(ans,sum);
		return;
	}
	for(int i=1;i<=n;i++)
		if(!vis[i]){
			vis[i]=1;
			for(int j=1;j<=n;j++)
				if(hv[i][j]&&vis[j]) b[i]+=b[j];
			sub19(x+1,sum+b[i]);
			b[i]=a[i];
			vis[i]=0;
		}
}
inline void sub_B(){
	sort(a+2,a+n+1);
	long long sum=a[1],ss=0;
	for(int i=2;i<=n;i++) ss+=a[i];
	for(int i=n;i>1;i--){
		sum+=a[i];
		ans=max(ans,sum*i+(ss-sum));
	}
	ans=max(ans,1ll*a[1]*n+ss);
}
int main(){
	freopen("light.cpp","r",stdin);
	freopen("light.out","w",stdout);
	read(n);
	for(int i=1;i<=n;i++) read(a[i]),b[i]=a[i];
	for(int i=1;i<n;i++){
		read(u),read(v);
		hv[u][v]=hv[v][u]=1;
		sb&=(u==1||v==1);
	}
	if(sb) sub_B();
	else sub19(1,0);
	printf("%lld",ans);
	return 0;
}

